//
//  HLLiveHeaderUserView.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveHeaderUserView.h"
#import "HLZegoLiveKit.h"
#import "HLLiveAudienceRequest.h"

@interface HLLiveHeaderUserView ()<UICollectionViewDelegate,UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) QMUILabel *numLabel;
@property (nonatomic, strong) NSArray *datasources;

@end

@implementation HLLiveHeaderUserView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorClear;
        [self setupView];
        self.datasources = @[];
    }
    return self;
}

- (void)setupView {

    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 4;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(26, 26);
        //设置代理，实现代理方法
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = UIColorClear;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HLLiveHeaderUserCell class] forCellWithReuseIdentifier:[HLLiveHeaderUserCell identifyCell]];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.right.mas_equalTo(-42);
        make.height.mas_equalTo(26);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-8);
    }];
    
    self.numLabel = [[QMUILabel alloc] init];
    _numLabel.text = @"0";
    _numLabel.textAlignment = NSTextAlignmentCenter;
    _numLabel.textColor = UIColorWhite;
    _numLabel.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.5];
    _numLabel.layer.masksToBounds = YES;
    _numLabel.layer.cornerRadius = 13;
    _numLabel.font = UIFontMake(12);
    [self addSubview:_numLabel];
    [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(38, 26));
        make.centerY.equalTo(_collectionView);
        make.right.mas_equalTo(0);
    }];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.datasources.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HLLiveHeaderUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLLiveHeaderUserCell identifyCell] forIndexPath:indexPath];
    cell.user = self.datasources[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)setUsers:(NSArray<ZegoUser *> *)users
{
    
    _users = users;
    _numLabel.text = [NSString stringWithFormat:@"%ld", users.count];
    [self refreshUserRequest];
    if (users.count < 3) {
        [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(8);
            make.right.mas_equalTo(-42);
            make.height.mas_equalTo(26);
            make.left.mas_equalTo((3-users.count)*26);
            make.bottom.mas_equalTo(-8);
        }];
    }
}


- (void)refreshUserRequest
{
    [[[HLLiveAudienceRequest alloc] initWithType:HLLiveAudienceLineIn room:HLZegoLiveKit.sharedInstance.female.liveRoomCode] startWithCompletionBlockWithSuccess:^(__kindof HLLiveAudienceRequest * _Nonnull request) {
        if (request.isSuccess) {
            self.datasources = request.userList;
            [self.collectionView reloadData];
        }
    } failure:nil];
}


@end
